﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyBehaviorTree;

public class Example : MonoBehaviour
{
    // dis 小于0后攻击
    BehaviorTreeBuilder BTB;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("start");
        BTB = new BehaviorTreeBuilder();
        BTB.behaviorTree.blackBoard.addData("dis", new BlackBoardData<float>(5f));

        var selector = BTB.buildActiveSelector();
        BTB.behaviorTree.root = selector;

        var sequence = BTB.buildSequence();

        var actionWalk = BTB.buildAction<walk>();

        var conditon = BTB.buildCondition(check);
        sequence.addChild(conditon);

        var actionAttack = BTB.buildAction<attack>();
        sequence.addChild(actionAttack);

        
        selector.addChild(sequence);
        selector.addChild(actionWalk);

    }

    // Update is called once per frame
    void Update()
    {
        BTB.behaviorTree.run();
    }

    private bool check(BlackBoard blackBoard)
    {
        var dis = blackBoard.getData<float>("dis");
        //Debug.Log("check" + (dis < 0));
        return dis < 0;
    }

    private class walk : Action
    {
        float time = 0;
        float dis = 0;

        public override void start()
        {
            base.start();
            dis = blackBoard.getData<float>("dis");
            time = 0;
        }
        public override State update()
        {
            if (time < 1)
            {
                time += Time.deltaTime;
                return State.RUNNING;
            }
            dis -= 1;
            time = 0;
            blackBoard.updateData("dis", dis);
            Debug.Log("go"+dis);
            return State.RUNNING;
        }

        public override void end()
        {
            base.end();
        }
    }

    private class attack : Action
    {
        public override void start()
        {
            Debug.Log("attack!");
            base.start();
        }
        public override State update()
        {
            return base.update();
        }
        public override void end()
        {
            base.end();
        }
    }
}
